package com.frame.aop;

import com.frame.domain.SrcProcess;
import com.frame.domain.SrcProcessLog;
import com.frame.mycommon.Mycommon;
import com.frame.service.SrcProcessLogService;
import com.frame.service.SrcProcessService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;

@Component
@Aspect
public class MyProcessAop {

    @Autowired
    HttpServletRequest request;

    @Autowired
    SrcProcessLogService srcProcessLogService;

    @Around("@annotation(com.frame.mycommon.Mycommon)")
    public Object getobject(ProceedingJoinPoint point) throws Throwable {
        SrcProcessLog srcProcessLog = new SrcProcessLog();
        //操作时间
        srcProcessLog.setCreateTime(new Date());

        Object[] args = point.getArgs();
        String s = Arrays.toString(args);
        srcProcessLog.setProcessParam(s);

        //操作地点
        srcProcessLog.setProcessIp(request.getRemoteAddr());
        //请求url
        srcProcessLog.setProcessUrl(request.getRequestURI().toString());
        //请求方法
        srcProcessLog.setRequestMethod(request.getMethod());
        //方法名称
        String name = point.getSignature().getName();
        srcProcessLog.setMethod(name);

        MethodSignature signature = (MethodSignature) point.getSignature();
        Mycommon mycommon = signature.getMethod().getAnnotation(Mycommon.class);

        //业务类型
        srcProcessLog.setBusinessType(mycommon.type());
        //模块标题
        srcProcessLog.setTitle(mycommon.value());

        srcProcessLogService.add(srcProcessLog);

        return point.proceed();

    }

}
